package com.hao.tool.extra.simple;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.hao.tool.extra.cloud.module.ali.AliCloudManager;
import com.hao.tool.extra.cloud.module.ali.entity.AlCosDTO;
import com.hao.tool.extra.cloud.module.ali.entity.AlCosResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * description
 *
 * @author HaoZ on CMHK.gz
 * @date 2020-11-05 14:31
 */
public class AliCosUploadUtil {

    /**
     * 本地资源文件地址
     */
    public String sourcePath;

    /**
     * 本地日志文件地址
     */
    public String logFilePath;

    private int count = 0;

    public AliCosUploadUtil(String sourcePath) {
        this.sourcePath = sourcePath;
    }

    public AliCosUploadUtil(String sourcePath, String logFilePath) {
        this.sourcePath = sourcePath;
        this.logFilePath = logFilePath;
    }

    /**
     * @param file 本地文件根路径
     * @param cosPath  cos上传路径
     * @param alCosDTO cos实体信息
     * @throws FileNotFoundException
     */
    public void uploadDefDir(File file, String cosPath, AlCosDTO alCosDTO, boolean isConDir) throws FileNotFoundException {
        // 设置默认工厂类
        System.setProperty("org.apache.commons.logging.LogFactory", "org.apache.commons.logging.impl.LogFactoryImpl");
        // 设置日志打印类
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
//        设置默认日志级别
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.simplelog.defaultlog", "info");
//        System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info");
        if (file.exists()) {
            if (file.isDirectory()) {
                for (File listFile : Objects.requireNonNull(file.listFiles())) {
                    uploadDefDir(listFile, cosPath, alCosDTO, isConDir);
                }
            } else {
                String parentDir = file.getAbsoluteFile().getParent();
                parentDir = parentDir.replace(sourcePath, "");
                if (isConDir) {
                    cosPath = cosPath + parentDir;
                }
                AlCosResult alCosResult = AliCloudManager.buildCos(null, alCosDTO)
                        .updateFile(cosPath + "/", file.getName().toLowerCase(), new FileInputStream(file));

                String topStr = file.getAbsolutePath();
                if (StringUtils.isNotBlank(sourcePath)) {
                    topStr = topStr.replace(sourcePath, "");
                }
                topStr = "[" + (++count) + "][" + topStr + "] ";
                topStr = String.format("%-35s", topStr);
                String logContent = topStr + " ======> : " + (alCosResult.getAccessUrl());
                System.out.println(logContent);

                if (StringUtils.isNotBlank(logFilePath)) {
                    String dateStr = DateUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_PATTERN);
                    FileUtil.appendUtf8String("\n[" + dateStr + "]" + logContent, logFilePath);
                }

            }
        }
    }
}
